#!/usr/bin/env python3
# vim: tabstop=4 shiftwidth=4 expandtab

import antennaDB
import argparse
import datetime
import signoffs
import html

db = antennaDB.AntennaDB()

def printPage(pagetext, output=None):
    if output:
        outputfile = open(output, "w")
        outputfile.write(pagetext)
        outputfile.close()
    else:
        print(pagetext)

# Update statsfile
def updateStatFile(urlNr, entryNr, output=None):
    outputString = f"{datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')}\t{urlNr}\t{entryNr}\t%.2f"%(entryNr/urlNr)
    if output:
        outputfile = open(output, "a")
        outputfile.write(f"{outputString}\n")
        outputfile.close()
    else:
        print(outputString)
        
# Generate index page
def generateFeedPage(entries, output=None):
    pagetext='''# Antenna
## Receiving Transmissions From Geminispace
=> about.gmi What is this?
=> submit Send transmission!
'''
    datestamp = "0000-00-00"
    for entry in entries:
        timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%d')
        if not datestamp == timestamp:
            datestamp = timestamp
            pagetext += "\n"
        pagetext += "=> " + entry.link + " " + timestamp + " " + entry.author + ": " + entry.title + "\n"

    pagetext += f"\n> {signoffs.getsig()}\n"

    pagetext += '''
=> log Tail the log
=> twtxt.gmi Antenna's twtxt page.
=> atom.xml Antenna's Atom feed.
=> filter Customize your Antenna view.
=> stats.tsv Check out the latest statistics in tsv format.
'''
    printPage(pagetext, output)

def generateAtomFeed(entries, output=None):
    pagetext=f'''<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>Antenna</title>
  <id>gemini://warmedal.se/~antenna/</id>
  <updated>{datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')}</updated>
  <subtitle>Receiving transmissions from geminispace</subtitle>
  <link href="gemini://warmedal.se/~antenna/" rel="alternate"></link>
  <link href="gemini://warmedal.se/~antenna/atom.xml" rel="self"></link>
'''
    for entry in entries:
        timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%dT%H:%M:%SZ')
        pagetext += f'''  <entry>
    <id>{html.escape(entry.link)}</id>
    <title>{html.escape(entry.title)}</title>
    <updated>{timestamp}</updated>
    <link href="{html.escape(entry.link)}" rel="alternate"></link>
    <author>
      <name>{html.escape(entry.author)}</name>
    </author>
  </entry>
'''
    pagetext += "</feed>"
    printPage(pagetext, output) 

def generateTwtxtPage(entries, output=None):
    pagetext='''# Antenna Twtxt
=> twtxt.txt Plain text version

'''
    for entry in entries:
        pagetext += f"{entry.author} {entry.feedurl}\n> {datetime.datetime.utcfromtimestamp(entry.posted).strftime('%Y-%m-%dT%H:%M:%SZ')} {entry.twt}\n\n"

    pagetext += f"\n=> log Tail the log\n\n> {signoffs.getsig()}\n"

    printPage(pagetext, output)

def generateTwtxtFile(entries, output=None):
    pagetext = ""

    for entry in entries:
        pagetext += f"{entry.author}\t{entry.feedurl}\t{datetime.datetime.utcfromtimestamp(entry.posted).strftime('%Y-%m-%dT%H:%M:%SZ')}\t{entry.twt}\n"

    printPage(pagetext, output)

